package com.wang.tool.monitor.ops;

import com.wang.tool.monitor.LinkLogUtil;
import com.wang.tool.monitor.core.MonitorInfo;
import com.wang.tool.util.SimpleLogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 监控操作中心
 * 注意:业务依赖强度
 * @Author shaopeng
 * @Date 2022/10/11
 * @Version 1.0
 */
public class MonitorOpsCenter {

    /**
     * 配置
     * group#biz#action->ops
     */
    private static Map<String, MonitorOps> opsMap = null;


    /**
     * 取配置
     *
     * @param group
     * @param biz    非空
     * @param action 非空
     * @param time   时间戳
     * @return
     * @see MonitorInfo#MonitorInfo
     */
    public static MonitorOps getOps(String group, String biz, String action, long time) {
        if (opsMap == null || biz == null || action == null) {
            return null;
        }
        try {
            MonitorOps ops = opsMap.get((group == null ? "" : group) + "#" + biz + "#" + action);
            if (ops == null) {
                return null;
            }
            Long beginTime = ops.getBeginTime();
            if (beginTime != null && time < beginTime) {
                return null;
            }
            Long endTime = ops.getEndTime();
            if (endTime != null && time > endTime) {
                return null;
            }
            return ops;
        } catch (Throwable e) {
            LinkLogUtil.newTagLog(MonitorInfo.TAG_MONITOR_ERROR)
                    .v1(biz + ":" + action)
                    .v2("getOps")
                    .addAttachment(MonitorInfo.ERROR, e)
                    .log();
            return null;
        }
    }


    /**
     * 注册
     *
     * @param group
     * @param action 非空
     * @param biz    非空
     * @param ops    ops 非空
     * @return
     */
    public synchronized static void setOps(String group, String biz, String action, MonitorOps ops) {
        if (ops == null || biz == null || action == null || ops.getSource() == null) {
            throw new IllegalArgumentException("biz/action/ops can not be null");
        }
        if (ops.getBeginTime() != null && ops.getEndTime() != null && ops.getBeginTime() >= ops.getEndTime()) {
            throw new IllegalArgumentException("ops time range is invalid");
        }
        String key = (group == null ? "" : group) + "#" + biz + "#" + action;
        if (opsMap == null) {
            opsMap = new HashMap<>(200);
        }

        MonitorOps old = opsMap.get(key);
        if (old != null && ops.getSource().equals(old.getSource())) {
            SimpleLogUtil.info("reRegister monitorOps ignore, source equals, key:" + key);
            return;
        }
        opsMap.put(key, ops);
        SimpleLogUtil.info("[changeMonitorOps] key:{0}, newOps:{1}, oldOps:{2}", key, ops, old);
    }


    /**
     * 删除指定key的ops
     *
     * @param group
     * @param action 非空
     * @param biz    非空
     */
    public synchronized static void remove(String group, String biz, String action) {
        if (biz == null || action == null) {
            throw new IllegalArgumentException("biz/action can not be null");
        }
        String key = (group == null ? "" : group) + "#" + biz + "#" + action;
        if (opsMap == null) {
            return;
        }
        opsMap.remove(key);
        SimpleLogUtil.info("[removeMonitorOps] key:{0}", key);
    }


    /**
     * 清空所有ops
     */
    public synchronized static void clear() {
        SimpleLogUtil.info("[clearMonitorOps]");
        opsMap = null;
    }


    /**
     * 拷贝一份ops
     * 用于筛选&小变更配置
     *
     * @return
     */
    public static Map<String, MonitorOps> copyOpsStorage() {
        return new HashMap<>(opsMap);
    }

}
